import uicls
import trinity
import triui
import blue
import os.path
import corebrowserutil
import sys

class CoreBrowserPane(uicls.Container):
    __guid__ = 'browser.CoreBrowserPane'

    def Startup(self, *args):
        self.texture = trinity.TriTexture()
        self.texture.pixels = 'res:/UI/Texture/none.dds'
        self.isTabStop = True
        self.cursor = 0
        self.browserSessionSurfaceManager = None
        self.browserSession = None
        self.surface = None
        self.textureAwaitingSwap = None
        self.textureWidth = 0
        self.textureHeight = 0
        self.renderJob = trinity.CreateRenderJob('Browser')
        self.renderJob.SetStdRndStates(trinity.RM_SPRITE2D)
        self.renderTextureStep = self.renderJob.RenderTexture(None)
        self.renderJob.ScheduleRecurring(self.scheduledRecurring)
        trinity.device.RegisterResource(self)
        self.StartBrowserView()



    def StartBrowserView(self):
        self.AdjustTextureSize()



    def SetCursor(self, cursorType):
        self.cursor = cursorType



    def OnSetFocus(self, *args):
        if (self.browserSession is not None):
            self.browserSession.OnSetFocus(args)
            sm.GetService('ime').SetFocus(self)



    def OnKillFocus(self, *args):
        if (self.browserSession is not None):
            self.browserSession.OnKillFocus(args)
            sm.GetService('ime').KillFocus(self)



    def CheckFocusChange(self, browse):
        return True



    def AdjustTextureSize(self):
        (l, t, w, h,) = self.GetAbsolute()
        if ((w <= 0) or (h <= 0)):
            return 
        if (self.browserSession is not None):
            self.browserSession.SetBrowserSize(w, h)
        if ((w > self.textureWidth) or ((h > self.textureHeight) or ((w < (self.textureWidth / 2)) or (h < (self.textureHeight / 2))))):
            self.textureWidth = min(corebrowserutil.NextPowerOfTwo(max(w, self.AppGetMinWidth())), self.AppGetMaxWidth())
            self.textureHeight = min(corebrowserutil.NextPowerOfTwo(max(h, self.AppGetMinHeight())), self.AppGetMaxHeight())
            dev = trinity.device
            self.OnCreate(dev)
        if self.textureAwaitingSwap:
            return 
        self.renderTextureStep.brTexCoord = ((float(w) / self.textureWidth), (float(h) / self.textureHeight))



    def GetHint(self):
        if (self.browserSession is not None):
            return self.browserSession.hint
        else:
            return ''



    def OnCreate(self, device):
        texture = device.CreateTexture(self.textureWidth, self.textureHeight, 1, trinity.TRIUSAGE_DYNAMIC, trinity.TRIFMT_X8R8G8B8, trinity.TRIPOOL_DEFAULT)
        self.textureAwaitingSwap = texture
        self.surface = texture.GetSurfaceLevel(0)
        if (self.browserSession is not None):
            self.browserSession.SetBrowserSurface(self.surface, self._OnSurfaceReady)



    def _OnClose(self):
        uicls.Container._OnClose(self)
        self.browserSession = None
        self.renderTextureStep = None
        if (self.renderJob is not None):
            self.renderJob.UnscheduleRecurring(self.scheduledRecurring)
            self.renderJob = None



    def _OnSurfaceReady(self):
        if self.textureAwaitingSwap:
            (l, t, w, h,) = self.GetAbsolute()
            self.renderTextureStep.brTexCoord = ((float(w) / self.textureWidth), (float(h) / self.textureHeight))
            self.renderTextureStep.texture = self.textureAwaitingSwap
            self.textureAwaitingSwap = None



    def ResizeBrowser(self, *args):
        self.AdjustTextureSize()



    def OnBrowseTo(self):
        self.StartBrowserView()
        uicore.registry.SetFocus(self)



    def _OnKeyDown(self, vkey, flag):
        if (self.browserSession is not None):
            self.browserSession._OnKeyDown(vkey, flag)



    def _OnKeyUp(self, vkey, flag):
        if (self.browserSession is not None):
            self.browserSession._OnKeyUp(vkey, flag)



    def OnChar(self, char, flag):
        if (self.browserSession is not None):
            self.browserSession.OnChar(char, flag)
            return True



    def _OnMouseMove(self, *args):
        if (self.browserSession is not None):
            (l, t, w, h,) = self.GetAbsolute()
            x = (eve.triapp.uilib.x - l)
            y = (eve.triapp.uilib.y - t)
            self.browserSession._OnMouseMove(x, y)



    def _OnMouseDown(self, *args):
        if (self.browserSession is not None):
            self.browserSession._OnMouseDown(args[0])



    def _OnMouseUp(self, *args):
        if (self.browserSession is not None):
            self.browserSession._OnMouseUp(args[0])



    def _OnMouseWheel(self, *args):
        if (self.browserSession is not None):
            self.browserSession._OnMouseWheel(eve.triapp.uilib.dz)



    def GetSurface(self):
        return self.surface



    def AppGetMinWidth(self):
        return 16



    def AppGetMinHeight(self):
        return 16



    def AppGetMaxWidth(self):
        return 4096



    def AppGetMaxHeight(self):
        return 4096



    def SetLangIndicator(self, lang):
        return 



exports = {'browser.CoreBrowserPane': CoreBrowserPane}

